package com.tools.scanner.ui.qrcode.creator

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.core.content.FileProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.tools.scanner.BaseActivity
import com.tools.scanner.R
import com.tools.scanner.bean.BarcodeSchema
import com.tools.scanner.database.QrHistory
import com.tools.scanner.databinding.ActivityCreateQrBinding
import com.tools.scanner.utils.ImageUtil
import com.tools.scanner.utils.ToastUtil
import java.io.File

class CreateQrCodeActivity : BaseActivity() {

    companion object {

        private const val CREATE_HISTORY = "create_history"

        fun actionStart(context: Context, qrHistory: QrHistory) {
            val intent = Intent(context, CreateQrCodeActivity::class.java)
            intent.putExtra(CREATE_HISTORY, qrHistory)
            context.startActivity(intent)
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putSerializable(CREATE_HISTORY, mQrHistory)
    }

    private lateinit var mBinding: ActivityCreateQrBinding
    private lateinit var mBitmap: Bitmap
    private var mQrHistory: QrHistory? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityCreateQrBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        mQrHistory = if (savedInstanceState != null) {
            savedInstanceState.getSerializable(CREATE_HISTORY) as QrHistory?
        } else {
            intent.getSerializableExtra(CREATE_HISTORY) as QrHistory?
        }

        val qrHistory = mQrHistory
        if (qrHistory == null) {
            finish()
            return
        }

        mBinding.ivBack.setOnClickListener {
            finish()
        }

        mBinding.llSave.setOnClickListener {
            if (!this::mBitmap.isInitialized) {
                ToastUtil.showToast(R.string.save_fail)
                return@setOnClickListener
            }

            if (ImageUtil.saveToGallery(this, mBitmap)) {
                ToastUtil.showToast(R.string.save_success)
            } else {
                ToastUtil.showToast(R.string.save_fail)
            }
        }

        mBinding.llShare.setOnClickListener {
            val uri = FileProvider.getUriForFile(
                this,
                "${packageName}.fileprovider",
                File(qrHistory.photoPath)
            )
            val shareIntent = Intent().apply {
                action = Intent.ACTION_SEND
                putExtra(Intent.EXTRA_STREAM, uri)
                type = "image/png"
                addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }

            try {
                startActivity(Intent.createChooser(shareIntent, ""))
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        Glide.with(this)
            .asBitmap()
            .load(qrHistory.photoPath)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    mBitmap = resource
                    mBinding.imageView.setImageBitmap(mBitmap)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })
        updateUi(qrHistory.schema)

    }

    private fun updateUi(scheme: BarcodeSchema) {
        when (scheme) {
            BarcodeSchema.TEXT -> {
                mBinding.ivType.setImageResource(R.drawable.ic_text)
                mBinding.tvType.setText(R.string.scan_type_content)
            }

            BarcodeSchema.WIFI -> {
                mBinding.ivType.setImageResource(R.drawable.ic_wifi)
                mBinding.tvType.setText(R.string.scan_type_wifi)
            }

            BarcodeSchema.URL -> {
                mBinding.ivType.setImageResource(R.drawable.ic_link)
                mBinding.tvType.setText(R.string.scan_type_link)
            }

            BarcodeSchema.CONTACT -> {
                mBinding.ivType.setImageResource(R.drawable.ic_contact)
                mBinding.tvType.setText(R.string.scan_type_contact)
            }

            BarcodeSchema.SMS -> {
                mBinding.ivType.setImageResource(R.drawable.ic_sms)
                mBinding.tvType.setText(R.string.scan_type_sms)
            }

            BarcodeSchema.EMAIL -> {
                mBinding.ivType.setImageResource(R.drawable.ic_email)
                mBinding.tvType.setText(R.string.scan_type_email)
            }

            BarcodeSchema.CALENDER -> {
                mBinding.ivType.setImageResource(R.drawable.ic_calendar)
                mBinding.tvType.setText(R.string.scan_type_calendar)
            }

            BarcodeSchema.PHONE -> {
                mBinding.ivType.setImageResource(R.drawable.ic_phone)
                mBinding.tvType.setText(R.string.scan_type_phone)
            }

            BarcodeSchema.GEO -> {
                mBinding.ivType.setImageResource(R.drawable.ic_location)
                mBinding.tvType.setText(R.string.scan_type_geo)
            }

            BarcodeSchema.EAN_8 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.ean_8)
            }

            BarcodeSchema.EAN_13 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.ean_13)
            }

            BarcodeSchema.UPC_E -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.upc_e)
            }

            BarcodeSchema.UPC_A -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.upc_a)
            }

            BarcodeSchema.CODE_39 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.code_39)
            }

            BarcodeSchema.CODE_93 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.code_93)
            }

            BarcodeSchema.CODE_128 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.code_128)
            }

            BarcodeSchema.ITF -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.itf)
            }

            BarcodeSchema.PDF_417 -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.PDF417)
            }

            BarcodeSchema.CODABAR -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.codabar)
            }

            BarcodeSchema.DATA_MATRIX -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.data_matrix)
            }

            BarcodeSchema.AZTEC -> {
                mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                mBinding.tvType.setText(R.string.aztec)
            }
        }
    }

}